<?php
$device_obj = new device();
$project_obj = new project();

$action_ary = array(
	'improt_out' => 'device_act_improt_out', 
	'improt_car_out'=>'device_act_improt_car_out',  
	'file_del'=>'device_act_file_del',	
);


/**
 * 导出装车统计报表信息
 * @author leiqianyong 2015-10-19 10:21 AM 
 */
function device_act_improt_out(){
	global $device_obj,$project_obj;
	$data = $_GET;	
			
	$pid=$data['pid'];
	if(!$pid)exit(0);
				
	$now_time=time();
	//$now_time=strtotime('2017-03-16 '.date('H:i:s')); //模拟测试地址		
	$data['start_time']=date('Y-m-d',$now_time);
	$data['end_time']=date('Y-m-d H:i:s',$now_time);
		
	//新计算方式	
	$list=$device_obj->get_report_all_for_report($data,1);
	if(empty($list))exit(0);

    //获取挖机计时数据
    $list_time=$device_obj->get_act_time_report_parse($data);
    $ctime_ary=false;
    if($list_time){
        foreach($list_time as $key=>$cv){
            $time_sum=$cv['sum_time'];
            $ctime_ary[$key]=$time_sum;
            $ctime_rtn[$key]=$cv;
        }

    }


    //项目工时间隔
    $pinfo=$project_obj->info($data['pid'],'time_zc,config');
    $time_zc=($pinfo && $pinfo['time_zc']>0)?$pinfo['time_zc']:300;

    foreach($list as $key=>$cv){
        //$dname=$cv['dname']; //挖机编号
        $dname=md5($cv['dname'].'_'.$cv['jsno'].'_'.$cv['dkno']);

        //计时拍照模式
        if($cv['gcat']){
            $grtn[$dname]['gcat']=$grtn[$dname]['gcat']?($grtn[$dname]['gcat']+1):1;
            $grtn[$dname]['ginfo']=$cv;
            continue;
        }


        $rtn[$dname]['dname']=$cv['dname'];
        $rtn[$dname]['car_type']=$cv['car_type'];
        $rtn[$dname]['downer']=$cv['downer'];
        $rtn[$dname]['jsno']=$cv['jsno'];
        $rtn[$dname]['doper']=$cv['doper'];
        $rtn[$dname]['dkno']=$cv['dkno'];
        $key=$cv['truck'].'_'.$cv['dust'];
        $rtn[$dname][$key]=$rtn[$dname][$key]?($rtn[$dname][$key]+1):1;

        //累计时间
        //前面的记录
        $lastime=$rtn[$dname]['lastime']?$rtn[$dname]['lastime']:0;
        $grtime=$cv['ctime']-$lastime;
        //异常时间处理：小于等于0，大于等于13分钟，设置成标准时间
        if($grtime<=0 || $grtime>780)$grtime=$time_zc;
        $rtn[$dname]['altime']=$rtn[$dname]['altime']?($rtn[$dname]['altime']+$grtime):$grtime;
        $rtn[$dname]['lastime']=$cv['ctime']; //作为下次时间计算的上一条
    }


    //计时拍照模式的导入
    if($grtn){
        foreach($grtn as $key=>$vo){
            if($rtn[$key]){
                $rtn[$key]['gcnum']=$vo['gcat'];
            }else{
                $rtn[$key]=$vo['ginfo'];
                $rtn[$key]['gcnum']=$vo['gcat'];
            }
        }
    }


	$title_ext = '挖机装车统计信息表' ;
	$filename = "SYT_WJZC_".date('YmdHi') ;  //ⅠⅡⅢ

    $title = array('序号','挖机编号','设备型号','产权人','机手编号','机手姓名','地块编号','Ⅰ类A型','Ⅰ类B型','Ⅰ类C型','Ⅱ类A型','Ⅱ类B型','Ⅱ类C型','Ⅲ类A型','Ⅲ类B型','Ⅲ类C型','装车车数','装车工时','辅助计时','计时车数','合计车数','总工时','分钟/车','车/小时','备注','签字');
    if($pinfo['config']){
        $config=stringToarray($pinfo['config']);
        $a=$config['a']?$config['a']:'A';
        $b=$config['b']?$config['b']:'B';
        $c=$config['c']?$config['c']:'C';
        $x=$config['x']?$config['x']:'Ⅰ';
        $y=$config['y']?$config['y']:'Ⅱ';
        $z=$config['z']?$config['z']:'Ⅲ';
        $title[7]=$x.$a;
        $title[8]=$x.$b;
        $title[9]=$x.$c;
        $title[10]=$y.$a;
        $title[11]=$y.$b;
        $title[12]=$y.$c;
        $title[13]=$z.$a;
        $title[14]=$z.$b;
        $title[15]=$z.$c;
    }

    $cols_num = count($title);
    $html='<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40">
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
    <meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
    </head>
    <body>
    <table x:str border=1 cellpadding=0 cellspacing=0>';

    $html .= '<tr><td width=”100″ colspan='.$cols_num.' align=center style=\'font-size:14.0pt;font-weight:700;font-family:"Arial Unicode MS", sans-serif;height:25pt\'>'.$title_ext.'</td></tr>';

    //开始时间
    if($data['start_time'] || $data['end_time']){
        $html .= "<tr><td width=”100″  colspan='{$cols_num}'>&nbsp;&nbsp;&nbsp;&nbsp;统计期间：{$data['start_time']} ~ {$data['end_time']}</td></tr>";
    }

    //导出xls 开始
    if (!empty($title)){
        $html .= '<tr>' ;
        foreach ($title as $k => $v) {
            $html .= '<td width=”100″ align=center style=\'height:25pt;background:silver;\'>'.$v.'</td>' ;
        }
        $html .= '</tr>' ;
    }

    $_num = 0 ;
    $_tot = 0 ;	//装车数
    $_sum_time=0; //装车总时
    $_sum_time_fz=0; //辅助总时
    $_sum_jszc=0; //计时车数
    $_sum_all=0; //合计车数
    $_sum_time_all=0;//总工时
    if (!empty($rtn)){
        foreach($rtn as $key=>$cv){
            $_all = 0 ; //单位总车数
            $a1=$cv['a_1']?$cv['a_1']:0;
            $_all +=$a1;
            $a2=$cv['a_2']?$cv['a_2']:0;
            $_all +=$a2;
            $a3=$cv['a_3']?$cv['a_3']:0;
            $_all +=$a3;
            $b1=$cv['b_1']?$cv['b_1']:0;
            $_all +=$b1;
            $b2=$cv['b_2']?$cv['b_2']:0;
            $_all +=$b2;
            $b3=$cv['b_3']?$cv['b_3']:0;
            $_all +=$b3;
            $c1=$cv['c_1']?$cv['c_1']:0;
            $_all +=$c1;
            $c2=$cv['c_2']?$cv['c_2']:0;
            $_all +=$c2;
            $c3=$cv['c_3']?$cv['c_3']:0;
            $_all +=$c3;
            $gcnum=$cv['gcnum']?$cv['gcnum']:0;
            $_tot +=$_all;
            $al_time=secToTime($cv['altime']);
            $_sum_time+=$cv['altime'];

            //工时/车
            $per_time_car=($cv['altime']/60/$_all);
            $per_time_car=$per_time_car?sprintf("%.1f",$per_time_car):0;
            //车/工时
            $per_car_time=sprintf("%.1f",(60/$per_time_car));
            $per_car_time=$per_car_time?$per_car_time:1;
            //辅助计时
            $ctime=$ctime_ary[$key];
            $_sum_time_fz+=$ctime;
            $ctime_str=$ctime?secToTime($ctime):'';
            //总工时
            if(empty($ctime_str)){
                $al_time_str=$al_time;
            }else{
                unset($ctime_ary[$key]);
                $al_time_str=secToTime($cv['altime']+$ctime);
            }

            $_sum_time_all+=($cv['altime']+$ctime);
            //计时车数
            $_sum_jszc+=$gcnum;
            $_sum_all+=($gcnum+$_all);

            $html .= '<tr>' ;
            $html .= '<td width=”100″ align=center x:num>'.($_num+1).'</td>' ;
            $html .= '<td width=”100″ align=left x:str><strong>'.($cv['dname']).'</strong></td>' ;
            $html .= '<td width=”100″ align=left x:str>'.$cv['car_type'].'</td>' ;
            $html .= '<td width=”100″ align=left x:str>'.$cv['downer'].'</td>' ;
            $html .= '<td width=”100″ align=left x:str>'.$cv['jsno'].'</td>' ;
            $html .= '<td width=”100″ align=left x:str>'.$cv['doper'].'</td>' ;
            $html .= '<td width=”100″ align=left x:str>'.$cv['dkno'].'</td>' ;
            $html .= '<td width=”100″ align=right x:num>'.($a1).'</td>' ;
            $html .= '<td width=”100″ align=right x:num>'.($b1).'</td>' ;
            $html .= '<td width=”100″ align=right x:num>'.($c1).'</td>' ;
            $html .= '<td width=”100″ align=right x:num>'.($a2).'</td>' ;
            $html .= '<td width=”100″ align=right x:num>'.($b2).'</td>' ;
            $html .= '<td width=”100″ align=right x:num>'.($c2).'</td>' ;
            $html .= '<td width=”100″ align=right x:num>'.($a3).'</td>' ;
            $html .= '<td width=”100″ align=right x:num>'.($b3).'</td>' ;
            $html .= '<td width=”100″ align=right x:num>'.($c3).'</td>' ;
            $html .= '<td width=”100″ align=right x:num>'.($_all).'</td>';
            $html .= '<td width=”100″ align=right x:num>'.$al_time.'</td>';
            $html .= '<td width=”100″ align=right x:num>'.$ctime_str.'</td>';
            $html .= '<td width=”100″ align=right x:num>'.$gcnum.'</td>';
            $html .= '<td width=”100″ align=right x:num>'.($gcnum+$_all).'</td>';
            $html .= '<td width=”100″ align=right x:num>'.$al_time_str.'</td>';
            $html .= '<td width=”100″ align=right x:num>'.$per_time_car.'</td>';
            $html .= '<td width=”100″ align=right x:num>'.$per_car_time.'</td>';
            $html .= '<td width=”100″ align=right x:str></td>';
            $html .= '<td width=”100″ align=right x:str></td>';
            $html .= '</tr>' ;
            $_num++;
        }
    }


    //补充只有辅助计时的记录
    if($ctime_ary){
        foreach($ctime_ary as $key=>$vo){
            $cv=$ctime_rtn[$key];
            $a1=$a2=$a3=$b1=$b2=$b3=$c1=$c2=$c3=0;
            $_all=0;
            $al_time=0;
            $al_time_str=$ctime_str=secToTime($vo);
            $per_car_time=$per_time_car=0;
            $html .= '<tr>' ;
            $html .= '<td width=”100″ align=center x:num>'.($_num+1).'</td>' ;
            $html .= '<td width=”100″ align=left x:str><strong>'.($cv['dname']).'</strong></td>' ;
            $html .= '<td width=”100″ align=left x:str>'.$cv['car_type'].'</td>' ;
            $html .= '<td width=”100″ align=left x:str>'.$cv['downer'].'</td>' ;
            $html .= '<td width=”100″ align=left x:str>'.$cv['jsno'].'</td>' ;
            $html .= '<td width=”100″ align=left x:str>'.$cv['doper'].'</td>' ;
            $html .= '<td width=”100″ align=left x:str>'.$cv['dkno'].'</td>' ;
            $html .= '<td width=”100″ align=right x:num>'.($a1).'</td>' ;
            $html .= '<td width=”100″ align=right x:num>'.($b1).'</td>' ;
            $html .= '<td width=”100″ align=right x:num>'.($c1).'</td>' ;
            $html .= '<td width=”100″ align=right x:num>'.($a2).'</td>' ;
            $html .= '<td width=”100″ align=right x:num>'.($b2).'</td>' ;
            $html .= '<td width=”100″ align=right x:num>'.($c2).'</td>' ;
            $html .= '<td width=”100″ align=right x:num>'.($a3).'</td>' ;
            $html .= '<td width=”100″ align=right x:num>'.($b3).'</td>' ;
            $html .= '<td width=”100″ align=right x:num>'.($c3).'</td>' ;
            $html .= '<td width=”100″ align=right x:num>'.($_all).'</td>';
            $html .= '<td width=”100″ align=right x:num>'.$al_time.'</td>';
            $html .= '<td width=”100″ align=right x:num>'.$ctime_str.'</td>';
            $html .= '<td width=”100″ align=right x:num>0</td>';
            $html .= '<td width=”100″ align=right x:num>0</td>';
            $html .= '<td width=”100″ align=right x:num>'.$al_time_str.'</td>';
            $html .= '<td width=”100″ align=right x:num>'.$per_time_car.'</td>';
            $html .= '<td width=”100″ align=right x:num>'.$per_car_time.'</td>';
            $html .= '<td width=”100″ align=right x:str></td>';
            $html .= '<td width=”100″ align=right x:str></td>';
            $html .= '</tr>' ;
            $_num++;
        }
    }

    $html .= '<tr><td colspan="16" align=right>合计</td><td align="right">'.$_tot.'</td><td align="right">'.secToTime($_sum_time).'</td><td align="right">'.secToTime($_sum_time_fz).'</td>';
    $html .= '<td align="right">'.$_sum_jszc.'</td><td align="right">'.$_sum_all.'</td><td align="right">'.secToTime($_sum_time_all).'</td><td colspan="4"></td></tr>';
    $html .= '</body></html></table>';
	
	$dir="./data/report/".$pid."/wj/";
	if(!is_dir($dir))mkdir($dir,0777,true);
	$tfile=$filename.".xls";
	$outfile=$dir.$tfile;
	file_put_contents($outfile,$html);	
	echo trim($outfile,'\.');
}


/**
 * 渣车模式报表导出
 * @author leiqianyong 2016-3-16 16:58 PM 
 */
function device_act_improt_car_out(){
	global $device_obj,$project_obj;
	$data = $_GET;
	
	$pid=$data['pid'];
	if(!$pid)exit(0);	
	
	$now_time=time();
	//$now_time=strtotime('2017-03-19 '.date('H:i:s')); //模拟测试地址
	$data['start_time']=date('Y-m-d',$now_time);
	$data['end_time']=date('Y-m-d H:i:s',$now_time);	
	
	
	$list=$device_obj->_get_car_total_report_for_report($data);
	if(empty($list))exit(0);
			
		$title_ext = '渣车运输统计表' ;
		$filename = "SYT_ZCYS_".date('YmdHi') ;  //ⅠⅡⅢ
		
		//从总记录中统计数据
		foreach($list as $cv){
			$dname=$cv['dname'].'_'.$cv['line'].'_'.$cv['sjno'];
		
			$rtn[$dname]['dname']=$cv['dname'];
			$rtn[$dname]['car_type']=$cv['car_type'];
			$rtn[$dname]['sjno']=$cv['sjno'];
			$rtn[$dname]['doper']=$cv['doper'];
			$rtn[$dname]['line']=$cv['line'];
			$rtn[$dname]['sum']=($rtn[$dname]['sum']?$rtn[$dname]['sum']+1:1);
			if(empty($rtn[$dname]['start']))$rtn[$dname]['start']=$cv['ctime'];
			$rtn[$dname]['end']=$cv['ctime'];
		}
		
		$title = array('渣车牌号','车辆型号','司机编号','司机姓名','线路','车数','首车登记时间','尾车登记时间','司机签字');
		$cols_num = count($title);
		$html='<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40">
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
    <meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
    </head>
    <body>
    <table x:str border=1 cellpadding=0 cellspacing=0>';
		
		$html .= '<tr><td width=”100″ colspan='.$cols_num.' align=center style=\'font-size:14.0pt;font-weight:700;font-family:"Arial Unicode MS", sans-serif;height:25pt\'>'.$title_ext.'</td></tr>';
		
		//开始时间
		if($data['start_time'] || $data['end_time']){
			$html .= "<tr><td width=”100″ colspan='{$cols_num}'>&nbsp;&nbsp;&nbsp;&nbsp;统计期间：{$data['start_time']} ~ {$data['end_time']}</td></tr>";
		}
		
		//导出xls 开始
		if (!empty($title)){
			$html .= '<tr>' ;
			foreach ($title as $k => $v) {
				$html .= '<td width=”100″ align=center style=\'height:25pt;background:silver;\'>'.$v.'</td>' ;
			}
			$html .= '</tr>' ;
		}
		
		$_sum=0;
		foreach($rtn as $key=>$cv){
			$html .= '<tr>' ;
			$html .= '<td width=”100″ align=left x:str><strong>'.($cv['dname']).'</strong></td>' ;
			$html .= '<td width=”100″ align=left x:str><strong>'.($cv['car_type']).'</strong></td>' ;
			$html .= '<td width=”100″ align=left x:str>'.$cv['sjno'].'</td>' ;
			$html .= '<td width=”100″ align=left x:str>'.$cv['doper'].'</td>' ;
			$html .= '<td width=”100″ align=left x:str><strong>'.($cv['line']).'</strong></td>' ;
			$html .= '<td width=”100″ align=left x:num>'.$cv['sum'].'</td>' ;
			$html .= '<td width=”100″ align=right x:str>'.date('H:i',$cv['start']).'</td>' ;
			$html .= '<td width=”100″ align=right x:str>'.date('H:i',$cv['end']).'</td>' ;
			$html .= '<td width=”100″ align=right x:str></td>' ;
			$html .= '</tr>' ;
			$_sum+=$cv['sum'];
		}
		
		$html .= '<tr><td width=”100″ colspan='.$cols_num.' align=center>合计：&nbsp;&nbsp;'.$_sum.'</td></tr>';
		$html .= '<tr><td width=”100″ colspan='.$cols_num.' align=center>统计：&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;复核：&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;审核</td></tr>';
		$html .= '</body></html></table>';
		
		$dir="./data/report/".$pid."/zc/";
		if(!is_dir($dir))mkdir($dir,0777,true);
		$filename=$dir.$filename.".xls";
		file_put_contents($filename,$html);
		echo trim($filename,'\.');		
}


//执行远程命令删除文件
function device_act_file_del(){
	$dir = request('dir');
	$date = request('date');
	if(empty($dir) || empty($date))return false;
	
	//最多删除3月以前的数据
	$dtime=strtotime($date);
	$deltime=strtotime("-3 month");
	if($dtime>$deltime)return false;
	
	$deldir=base64_decode($dir);
	deldir($deldir,true);	
}

/**
 * 清空文件夹函数和清空文件夹后删除空文件夹函数的处理
 * @param string  $path 路径   './data/cs/'
 * @param boolean $deldir 是否删除主目录
 */
function deldir($path,$deldir=false){
	//如果是目录则继续
	if(is_dir($path)){
		//扫描一个文件夹内的所有文件夹和文件并返回数组
		$p = scandir($path);
		foreach($p as $val){
			//排除目录中的.和..
			if($val !="." && $val !=".."){
				//如果是目录则递归子目录，继续操作
				if(is_dir($path.$val)){
					//子目录中操作删除文件夹和文件
					deldir($path.$val.'/');
					//目录清空后删除空文件夹
					@rmdir($path.$val.'/');
				}else{
					//如果是文件直接删除
					unlink($path.$val);
				}
			}
		}
		 
		//删除当前文件夹
		if($deldir)@rmdir($path);
	}
}
